package cn.bdqn.service.impl;

import cn.bdqn.bean.SaltUtil;
import cn.bdqn.domain.Doctor;
import cn.bdqn.domain.Nurse;
import cn.bdqn.mapper.DoctorMapper;
import cn.bdqn.mapper.NurseMapper;
import cn.bdqn.service.LoginService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private NurseMapper nurseMapper;

    /**
     * 查询医生的登录信息
     * @param doctorNum
     * @return
     */
    @Override
    public Doctor queryDoctorByID(String doctorNum) {
        return doctorMapper.selectByDoctorNumber(doctorNum);
    }

    /**
     * 查询护士的登录信息
     * @param nurseNum
     * @return
     */
    @Override
    public Nurse queryNurseByID(String nurseNum) {
        return nurseMapper.selectByCard(nurseNum);
    }

    /**
     * 添加一条医生信息
     * @param doctor
     * @return
     */
    @Override
    public int addDoctor(Doctor doctor) {

        Doctor d = doctorMapper.selectByDoctorNumber(doctor.getDoctorNumber());
        if (d == null){
            doctor.setSalt(SaltUtil.getSalt(8));
            Md5Hash md5Hash = new Md5Hash(doctor.getDoctorPassword(),doctor.getSalt(),1024);

            doctor.setDoctorPassword(md5Hash.toHex());
            return doctorMapper.insertSelective(doctor);
        }
        return 0;

    }

    /**
     * 添加一条护士信息
     * @param nurse
     * @return
     */
    @Override
    public int addNurse(Nurse nurse) {

        Nurse n = nurseMapper.selectByCard(nurse.getCard());


        if (n == null){

            nurse.setSalt(SaltUtil.getSalt(8));
            Md5Hash md5Hash = new Md5Hash(nurse.getPassword(),nurse.getSalt(),1024);

            nurse.setPassword(md5Hash.toHex());

            return nurseMapper.insertSelective(nurse);
        }
       return 0;
    }
}
